package cn.com.jslh.boarpro.util.spinnerdialog;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import cn.com.jslh.boarpro.R;
import cn.jeesoft.widget.pickerview.CharacterPickerView;
import cn.jeesoft.widget.pickerview.CharacterPickerWindow;
import cn.jeesoft.widget.pickerview.OnOptionChangedListener;

/**
 * Created by lenovo on 2018/3/26.
 * 联动弹出框 两级
 */

public class OptionsWindowHelper {
    private Activity context;
    private static List<String> options1Items = null; //一级菜单
    private static List<List<String>> options2Items = null;  //二级菜单
    private static LinkedHashMap<String,List<String>> line;


    public OptionsWindowHelper(Activity context,LinkedHashMap<String,List<String>> line) {
        this.line = line;
        this.context=context;
        options2Items=new ArrayList<>();
        options1Items=new ArrayList<>();
    }

    public interface OnOptionsSelectListener {
        void onOptionsSelect(int option1, int option2);
    }


    public CharacterPickerWindow builder(final OnOptionsSelectListener listener) {
        //选项选择器
        CharacterPickerWindow mOptions = new CharacterPickerWindow(context);
        WindowManager manager = (context).getWindowManager();
        DisplayMetrics outMetrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(outMetrics);
        int width = outMetrics.widthPixels;
        int hight=outMetrics.heightPixels;
        mOptions.setWidth((int)(width*0.4));
        mOptions.setHeight((int)(hight*0.7));
        mOptions.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.popu_bg));

        //初始化选项数据
        setPickerData(mOptions.getPickerView());
        //设置默认选中的2级项目
        mOptions.setCurrentPositions(0,0);

        //监听确定选择按钮
        mOptions.setOnoptionsSelectListener(new OnOptionChangedListener() {
            @Override
            public void onOptionChanged(int option1, int option2, int option3) {
                if (listener != null) {
                    String op1 = options1Items.get(option1);
                    String op2 = options2Items.get(option1).get(option2);
                    listener.onOptionsSelect(option1, option2);
                }
            }
        });
        return mOptions;
    }


    /**
     * 初始
     */
    public void setPickerData(CharacterPickerView view) {
        if(line!=null){
            for (Map.Entry<String, List<String>> entry1 : line.entrySet()) {
                String key1 = entry1.getKey(); //一级菜单名称
                List<String> value1 = entry1.getValue();  //二级菜单
                options1Items.add(key1);
                options2Items.add(value1);
            }
            //两级联动效果
            view.setPicker(options1Items, options2Items);
        }


    }



    /**
     * 两级联动效果
     * @param window
     */
    public void setCurrentPositions(CharacterPickerWindow window, String op1, String op2) {
        int option1 = indexOf(options1Items, op1);
        int option2 = option1>=0 ? indexOf(options2Items.get(option1), op2) : -1;

        window.setCurrentPositions(option1, option2);
    }

    private int indexOf(List<String> data, String key) {
        if (data == null || data.isEmpty()) {
            return -1;
        }
        if (TextUtils.isEmpty(key)) {
            return -1;
        }
        return data.indexOf(key);
    }


    /*
    设置半透明
     */
    public  void setBackgroundAlpha(Activity activity, float bgAlpha) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        if (bgAlpha == 1) {
            activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        } else {
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        }
        activity.getWindow().setAttributes(lp);
    }




}
